package com.coocaa.ops.common.tool.feign;

import feign.*;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.StringDecoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;

/**
 * @author bjh
 * @description
 * @date 2020/12/22 19:55
 */
public class FeignUtil {
    private static final int CONNECT_TIMEOUT_MILLIS = 500;

    private static final int READ_TIMEOUT_MILLIS = 500;

    public static <T> T getFeignClient(Encoder encoder, Class<?> clazz) {
        T feignClient = Feign.builder()
                .encoder(encoder)
                .decoder(new StringDecoder())
                .contract(new SpringMvcContract())
                .options(new Request.Options(CONNECT_TIMEOUT_MILLIS, READ_TIMEOUT_MILLIS))
                .retryer(Retryer.NEVER_RETRY)
                .logLevel(Logger.Level.FULL)
                .target((Target<T>) Target.EmptyTarget.create(clazz));
        return feignClient;
    }

    public static <T> T getFeignClient(Encoder encoder, Class<?> clazz, int connectTimeOutMillis, int readTimeOutMillis) {
        T feignClient = Feign.builder()
                .encoder(encoder)
                .decoder(new StringDecoder())
                .contract(new SpringMvcContract())
                .options(new Request.Options(connectTimeOutMillis, readTimeOutMillis))
                .retryer(Retryer.NEVER_RETRY)
                .logLevel(Logger.Level.FULL)
                .target((Target<T>) Target.EmptyTarget.create(clazz));
        return feignClient;
    }

    public static <T> T getFeignClient(Encoder encoder, Decoder decoder, Class<?> clazz, int connectTimeOutMillis, int readTimeOutMillis) {
        T feignClient = Feign.builder()
                .encoder(encoder)
                .decoder(decoder)
                .contract(new SpringMvcContract())
                .options(new Request.Options(connectTimeOutMillis, readTimeOutMillis))
                .retryer(Retryer.NEVER_RETRY)
                .logLevel(Logger.Level.FULL)
                .target((Target<T>) Target.EmptyTarget.create(clazz));
        return feignClient;
    }
}
